// pages/mine/message/lists/index.js
const app = getApp();
var serverUrl = app.globalData.serverUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    lists:[],
    refresh:"上拉加载更多",
    page:2,
    isrefresh:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var uid = wx.getStorageSync('userInfo').uid;
    wx.request({
      url: serverUrl+'/api/v1/system/messagelist',
      method:"GET",
      dataType:"json",
      data:{uid:uid,limit:12},
      success:function(data){
        data = data.data;
        if (data.code == 200 && data.data.total > 0) {
          that.setData({
            lists:data.data.data
          })
          if (data.data.to >= data.data.total) {
            that.setData({
              isrefresh: false
            })
          }
        } else {
          wx.showToast({
            title: '最近没有收到消息',
            icon:"none",
            duration: 2000
          })
        }
      },
      error:function(data){
        wx.showToast({
          title: '服务器错误!',
          icon:'none',
          duration:2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 用户上拉加载
   */
  onReachBottom:function(options){
    var that = this;
    if(that.data.isrefresh){
      that.setData({
        refresh: "正在加载..."
      })
      var uid = wx.getStorageSync('userInfo').uid;
      wx.request({
        url: serverUrl + 'api/v1/system/messagelist',
        method: "GET",
        dataType: "json",
        data: {
          uid: uid,
          limit: 12,
          page: that.data.page
        },
        success:function(data){
          data = data.data;
          if(data.code == 200){
            that.setData({
              lists: that.data.lists.concat(data.data.data),
              page:that.data.page++
            })
            if(data.data.to >= data.data.total){
              that.setData({
                isrefresh:false
              })
            }
          }
        }
      })
    }
  },

  /**跳转详情**/
  navicatToDetail:function(options){
    let id = options.currentTarget.id;
    var that = this;
    let lists = that.data.lists;
    for(let key in lists){
      if(lists[key].id == id && lists[key].status == 0){
        lists[key].status = 1;
      }
    }
    that.setData({
      lists:lists
    })
    wx.navigateTo({
      url: '../detail/index?id='+id,
    })
  },

  /**一键标记已读**/
  sign:function(){
    var that = this;
    var uid = wx.getStorageSync("userInfo").uid;
    wx.request({
      url: serverUrl +'api/v1/system/messagesign/uid/'+uid,
      method:"GET",
      success:function(){
        let lists = that.data.lists;
        for (let key in lists) {
          if(lists[key].status == 0){
            lists[key].status = 1
          }
        }
        that.setData({
          lists:lists
        })
      },
      error:function(data){
        wx.showToast({
          title: '服务器错误!',
          duration:2000,
          icon:"none"
        })
      }
    })
  }

})